package com.shwread.android.qysw.activity.psychological;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.NewActionHelp;
import com.ciko.code.base.BaseActicity;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.bean.psychological.EvaluatingClassifyInfo;
import com.shwread.android.qysw10000229.R;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 心理评测列表页
 * Created by sss on 2017/10/9.
 */
public class EvaluatingChargeListActivity extends BaseActicity {

    private TabLayout evaluatingTab;
    private ViewPager evaluatingPager;
    private List<EvaluatingClassifyInfo> classifyInfoList;

    @Override
    protected void initTopBar() {
        enableNavLeftImg();
        setTitle("心理评测");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_evaluating_list);
    }

    @Override
    protected void initView() {
        evaluatingTab = findViewByID(R.id.evaluating_tab);
        evaluatingPager = findViewByID(R.id.evaluating_pager);

    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        //获取分类列表
        NewActionHelp.getQuestionnaireModule(this, new GetQuestionnaireModuleHandler());
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onBackPressed() {
        toMain();
    }

    private void toMain() {
        Intent intent = new Intent(this, EvaluatingHomeActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        if (v == rect_imgNavBack) {
            toMain();
        }
    }

    private class GetQuestionnaireModuleHandler extends ObjectHttpResponseHandler<List<EvaluatingClassifyInfo>> {
        @Override
        public void onFailure(int i, Header[] headers, String responseString, Throwable throwable) {
            Log.e("sss", "失败---" + responseString);
        }

        @Override
        public void onSuccess(int i, Header[] headers, List<EvaluatingClassifyInfo> classifyInfos, String responseString) {
            Log.e("sss", "成功---" + responseString);
            if (classifyInfos == null || classifyInfos.size() == 0) {
                return;
            }
            classifyInfoList = classifyInfos;

            evaluatingPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
            evaluatingTab.setupWithViewPager(evaluatingPager);
        }

        @Override
        public Type getType() {
            return new TypeToken<List<EvaluatingClassifyInfo>>() {
            }.getType();
        }
    }

    private class MyAdapter extends FragmentPagerAdapter {
        @Override
        public CharSequence getPageTitle(int position) {
            return classifyInfoList.get(position).getTagName();
        }

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return EvaluatingListFragment.newInstance(classifyInfoList.get(position).getId());
        }

        @Override
        public int getCount() {
            return classifyInfoList == null ? 0 : classifyInfoList.size();
        }
    }
}
